package com.polly.esphwevent.common.disruptor;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;

/**
 * @title: DisruptorConfig
 * @description: TODO(用一句话描述该文件做什么)
 * @date: 2019/2/11 17:35
 * @author ldz112@163.com
 * @version V1.0
 */

@Configuration
public class DisruptorConfig {

    @Bean(name = "homeworkDisruptor")
    public Disruptor<HomeworkEvent> homeworkDisruptor() {
        int ringBufferSize = 1024 * 1024;
        Disruptor<HomeworkEvent> disruptor =
                new Disruptor<HomeworkEvent>(
                        new HomeworkEventFactory(),
                        ringBufferSize,
                        Executors.defaultThreadFactory(),
                        ProducerType.SINGLE,//MULTI
                        new BlockingWaitStrategy()
                );
        // 连接消费事件方法
        disruptor.handleEventsWithWorkerPool(new HomeworkEventHandler());


        disruptor.start();
        return disruptor;

    }

    //生产者
    @Bean
    public HomeworkEventProducer homeworkEventProducer(Disruptor<HomeworkEvent> disruptor) {
        return new HomeworkEventProducer(disruptor.getRingBuffer());
    }


}
